package com.david.equipment.controller;

import com.david.equipment.pojo.entity.FieldEntity;
import com.david.equipment.service.FieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/equipment/field")
@Slf4j
@RequiredArgsConstructor
@Api(tags = "字段管理")
public class FieldController {
//    @Autowired
//    @NonNull FieldService fieldService; 也可以用，autowired替代方案
    final FieldService fieldService;  //autowired替代方案,需要注解@RequiredArgsConstructor

    @PostMapping
    @ApiOperation(value = "查询字段")
    public ResponseEntity<Object> getFields(@RequestBody(required = false) FieldEntity field) {
        log.info("查询所有自定义字段：{}",field);
        List<FieldEntity> fieldEntityList = fieldService.getFields(field);
        return success(true, fieldEntityList);
    }

    @PutMapping
    public ResponseEntity<Object> addField(@RequestParam("name") String name, @RequestParam("type") String type) {
        log.info("新增自定义字段，类型{}，名称{}", type, name);
        Boolean result = fieldService.addField(name,type);
        return success(result, result?"新增成功":"名字重复，请检查");
    }

    @PutMapping("/edit")
    public ResponseEntity<Object> editField(@RequestBody FieldEntity field) {
        log.info("修改自定义字段{}", field);
        Boolean result = fieldService.editField(field);
        return success(result,result?"修改成功":"名字重复，请检查");
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Object> deleField(@PathVariable Integer id){
        log.info("删除自定义字段{}",id);
        fieldService.delField(id);
        return success(true, "删除成功");
    }
}
